package com.stlm2.dems.service;

import com.stlm2.core.common.page.PageView;
import com.stlm2.dems.dao.UserDao;
import com.stlm2.dems.entity.IndexConfigType;
import com.stlm2.dems.entity.User;
import com.stlm2.util.Memcached;
import com.stlm2.util.codecontent.CodeContent;
import org.apache.commons.lang.math.NumberUtils;
import org.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;

import com.stlm2.dems.entity.IndexConfig;
import com.stlm2.dems.dao.IndexConfigDao;
import com.stlm2.core.base.BaseDao;
import com.stlm2.core.base.impl.BaseServiceImpl;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("indexConfigService")
@Transactional(rollbackForClassName={"java.lang.Exception"}, propagation = Propagation.REQUIRED)
public class IndexConfigService extends BaseServiceImpl<IndexConfig> {

	@Resource
	private IndexConfigDao indexConfigDao;
	@Resource
	private UserDao userDao;
	@Resource
	private Memcached memcached;

	@Override
	protected BaseDao <IndexConfig> getDao() {
		return indexConfigDao;
	}
/*
*后台查询首页配置列表
* zlq
 */
	public String getList(ModelMap model ,String page,String typeValue,String token){
		PageView pageview = new PageView();
		int pageNo = 1;
		int pageSize = 20;
		if (NumberUtils.isDigits(page) && !"0".equals(page.trim())) {
			pageNo = NumberUtils.toInt(page);
		}
		if (StringUtils.isEmpty(token)) {
			return CodeContent.getModelByCode(-1, "", model);
		}
		User loginUser = userDao.getByToken(token);
		if (loginUser == null) {
			return CodeContent.getModelByCode(-1, "", model);
		}
		if (!NumberUtils.isDigits(typeValue)) {
			return CodeContent.getModelByCode(0, "类型不能为空", model);
		}
		if(IndexConfigType.getByIntValue(Integer.parseInt(typeValue))==null){
			return CodeContent.getModelByCode(0, "类型不存在", model);
		}
		IndexConfig indexConfig=new IndexConfig();
		indexConfig.setTypeValue(Integer.parseInt(typeValue));
		pageview=indexConfigDao.queryPageView(pageNo,pageSize,indexConfig);
		model.put("code",1);
		model.put("msg","success");
		if (pageview != null){
			model.put("datas",convertList(pageview.getRecords()));
			model.put("recordsTotal", pageview.getRowCount());
			model.put("totalPage", pageview.getPageCount());
			model.put("pageNow", pageview.getPageNow());
		}

		return new JSONObject(model).toString();
	}

	public List<ModelMap> convertList(List<IndexConfig> indexConfigList) {
		List<ModelMap> list = new ArrayList<ModelMap>();
		if (indexConfigList != null && indexConfigList.size() > 0) {
			for (IndexConfig indexConfig : indexConfigList) {
				ModelMap map = buildModelList(indexConfig);
				list.add(map);
			}
		}
		return list;
	}

	public ModelMap buildModelList(IndexConfig indexConfig) {
		ModelMap model = new ModelMap();
		model.put("id",indexConfig.getId());
		model.put("name",indexConfig.getName()!=null?indexConfig.getName():"");
		model.put("typeValue",indexConfig.getTypeValue());
		model.put("type",indexConfig.getType().toString());
		model.put("state",indexConfig.getState()?"有效":"无效");
		model.put("stateValue",indexConfig.getState()?"1":"0");
		model.put("img",indexConfig.getImg()!=null?indexConfig.getImg():"");
		model.put("url",indexConfig.getUrl()!=null?indexConfig.getUrl():"");
		model.put("number",indexConfig.getNumber());
		model.put("memo",indexConfig.getMemo()!=null?indexConfig.getMemo():"");
		return  model;
	}

	/*
	*修改首页配置
	 */
	public String modification(ModelMap model,String token ,String id ,String name,String img ,String url,String memo,boolean state){
		if (StringUtils.isEmpty(token)) {
			return CodeContent.getModelByCode(-1, "", model);
		}
		User loginUser = userDao.getByToken(token);
		if (loginUser == null) {
			return CodeContent.getModelByCode(-1, "", model);
		}
		if (!NumberUtils.isDigits(id)) {
			return CodeContent.getModelByCode(0, "id不能为空", model);
		}
		IndexConfig indexConfig=indexConfigDao.getById(Integer.parseInt(id));
		if(indexConfig==null){
			return CodeContent.getModelByCode(0, "记录不存在", model);
		}
		if(state){
			IndexConfig indexConfig2=new IndexConfig();
			indexConfig2.setState(true);
			indexConfig2.setType(indexConfig.getType());
			List <IndexConfig> indexConfigList= indexConfigDao.queryPageView(-1,-1,indexConfig2).getRecords();
			int size=indexConfigList.size();
			for(IndexConfig indexConfig3:indexConfigList){
				if(indexConfig3.getId()==indexConfig.getId()){
					size=size-1;
				}
			}
			if(size>3){
				return CodeContent.getModelByCode(0, "最多只允许有4个有效的设置", model);
			}

		}
		indexConfig.setName(name);
		indexConfig.setImg(img);
		indexConfig.setUrl(url);
		indexConfig.setMemo(memo);
		indexConfig.setState(state);
		indexConfigDao.modify(indexConfig);
		return CodeContent.getModelByCode(1, "", model);
	}
/*
*获取首页所有配置信息
* zlq
 */
	public String getConfig(ModelMap model,Boolean state){
		IndexConfig indexConfig=new IndexConfig();
		indexConfig.setState(state);
		indexConfig.setType(IndexConfigType.IndexLabel);
		List <IndexConfig> indexConfigList1=indexConfigDao.queryPageView(-1,-1,indexConfig).getRecords();
		indexConfig.setType(IndexConfigType.IndexSlideshow);
		List <IndexConfig> indexConfigList2=indexConfigDao.queryPageView(-1,-1,indexConfig).getRecords();
		indexConfig.setType(IndexConfigType.functionMenu);
		List <IndexConfig> indexConfigList3=indexConfigDao.queryPageView(-1,-1,indexConfig).getRecords();
		Map<String,Object> datas = new HashMap<String, Object>();
		datas.put("indexLabel",convertList(indexConfigList1));
		datas.put("indexSlideshow",convertList(indexConfigList2));
		datas.put("functionMenu",convertList(indexConfigList3));

		model.put("code",1);
		model.put("msg","success");
		model.put("datas",datas);

		return  new JSONObject(model).toString();
	}

	public String getDetails(ModelMap model,String token,String id){
		if (StringUtils.isEmpty(token)) {
			return CodeContent.getModelByCode(-1, "", model);
		}
		User loginUser = userDao.getByToken(token);
		if (loginUser == null) {
			return CodeContent.getModelByCode(-1, "", model);
		}
		if (!NumberUtils.isDigits(id)) {
			return CodeContent.getModelByCode(0, "id不能为空", model);
		}
		IndexConfig indexConfig=indexConfigDao.getById(Integer.parseInt(id));
		if(indexConfig==null){
			return CodeContent.getModelByCode(0, "记录不存在", model);
		}
		return CodeContent.getModelByCode(1, "", buildModelList(indexConfig));

	}






}